/**
 * Created by Xiaoming on 2017/9/4.
 */
angular.module('module.menu', [
        'service.wxauth',
        'service.auth',
        'service.ssoauth',
        'service.ssouser',
        'service.shopmenu',
        'service.privilege',
        'service.goods',
        'service.coupon',
        'service.order',
        'service.desk',
        'service.wxjssdk',
        'service.shop',
        'filter.order',
        'filter.coupon'
    ])

    .config(['$stateProvider',
        function ($stateProvider) {
            $stateProvider

                .state('store', {
                    url: '/store/:shopid?appid&deskid&code',
                    templateUrl: shopmenu_viewurl + '/menu/store.html',
                    //resolve: {
                    //    wxuser: wxuser
                    //},
                    controller: 'StoreController',
                    controllerAs: 'vm'
                })
                .state('menu', {
                    url: '/menu/:shopid?appid&deskid',
                    params: {
                        order: null,
                    },
                    templateUrl: shopmenu_viewurl + '/menu/menu.html',
                    resolve: {
                        //wxconfig: wxconfig,
                        deskList: deskList
                    },
                    controller: 'MenuController',
                    controllerAs: 'vm'
                })
        }])
wxuser.$inject = ['$q', '$stateParams', '$cookies', 'WxAuthService', 'AuthUserService', 'UserinfoService'];
function wxuser($q, $stateParams, $cookies, WxAuthService, AuthUserService, UserinfoService) {
    var deferred = $q.defer();
    var appid = $stateParams.appid;
    if (!appid) {
        appid = mall_userauth.wxmp_appid;
    } else {
        mall_userauth.wxmp_appid = appid;
    }
    if (mall_userauth.wxmp_appid != $cookies.get("appid")) {
        //判断不同的公众号进入从新缓存appid,移除用id缓存
        $cookies.put("appid", mall_userauth.wxmp_appid);
        $cookies.remove("_userId")
    }
    // 已登录
    /*if ($cookies.get("_userId")) {
        return $cookies.get("_userId");
    }*/
    var userid = $cookies.get("_userId");
    if (userid) {
        UserinfoService.getByUid(userid,function(ssouser){
            if(ssouser.phone){
                $cookies.put("phone",ssouser.phone);
            }else{
                $cookies.remove("phone");
            }
        },function(){

        });
        UserinfoService.getxwmUser(userid).$promise.then(function (user) {
            deferred.resolve(user);
            return userid;
            //deferred.reject(user);
        }, function (error) {
            //外卖用户未找到信息
            //不是微信链接地址跳转到微信链接地址
            $cookies.remove("_userId");
            var redirect_uri = window.location.href;
            WxAuthService.wxOpenidJump(redirect_uri);
            //deferred.resolve(error);
            deferred.reject(error);
        });
    }
    //尝试通过code后台登陆
    else if ($stateParams.code) {
        function success_cbfunc(userinfo) {
            UserinfoService.getByUid(userinfo.userid,function(ssouser){
                if(ssouser.phone){
                    $cookies.put("phone",ssouser.phone);
                }else{
                    $cookies.remove("phone");
                }
            },function(){

            });
            //尝试登陆成功缓存用户信息
            UserinfoService.getxwmUser(userinfo.userid).$promise.then(function (user) {
                deferred.resolve(user);
                if(user.phone){
                    if (!user.nickname || !user.logo) {
                        var redirect_uri = window.location.href.replace("/store/" + $stateParams.shopid + "?", "/wxuc?shopid=" + $stateParams.shopid + "&");
                        WxAuthService.wxAuthJump(redirect_uri);
                    }
                    //成功缓存用户信息
                    AuthUserService.addUserToCookies(user);
                }
            }, function (error) {
                //外卖用户未找到信息
                UserinfoService.addxwmUser(userinfo);
                deferred.resolve(error);
            });
        }

        function error_cbfunc(error) {
            deferred.resolve(error);
        }

        WxAuthService.wxTryLogin(UserinfoService.accountCheck, success_cbfunc, error_cbfunc);
    } else {
        //不是微信链接地址跳转到微信链接地址
        var redirect_uri = window.location.href;
        WxAuthService.wxOpenidJump(redirect_uri);
    }
    return deferred.promise;
}
wxconfig.$inject = ['$stateParams', 'WxjssdkService'];
function wxconfig($stateParams, WxjssdkService) {
    var url = window.location.href;
    var trueurl = url;
    if (url.indexOf("#") > 0) {
        trueurl = url.substring(0, url.indexOf("#"));
    }
    var appid = $stateParams.appid;
    if (!appid) {
        appid = mall_userauth.wxmp_appid;
    } else {
        mall_userauth.wxmp_appid = appid;
    }
    trueurl = trueurl.replace("store", "menu");
    trueurl = trueurl.replace("&scope=true", "");
    var postinfo = {
        "component_appid": mall_userauth.component_appid,
        "mer_id": appid,
        "path": trueurl
        // "path": "http://o2o.shuwang.info/orderinfo/store/476429753?appid=wxa255fef7bd52511d&deskid=8 "
    };
    var date = WxjssdkService.get_ticket(postinfo);
    // alert(JSON.stringify(date.$promise))
    return date.$promise;
}

deskList.$inject = ['$stateParams', 'DeskService'];
function deskList($stateParams, DeskService) {
    return DeskService.get_desk1($stateParams.shopid).$promise;
}